package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.enums.ReturnCodeEnum;
import cn.com.thinvent.dispatch.entity.file.filevo;
import cn.com.thinvent.dispatch.entity.file.contentVo;
import cn.com.thinvent.dispatch.entity.response.ReturnModel;
import cn.com.thinvent.dispatch.service.biz.BaseBiz;
import cn.com.thinvent.dispatch.service.biz.impl.PdfFileImpl;
import cn.com.thinvent.dispatch.web.utils.PdfboxUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * 健康监测controller
 */
@Api(description = "pdf转化")
@Controller
@RequestMapping("/pdfToText")
@ResponseBody
@Slf4j
@CrossOrigin(origins = "*")
public class HealthController {
    @Autowired
    private PdfFileImpl pdfimpl;
    @Autowired
    private BaseBiz baseBiz;

    //废弃方法，用拼音还不用驼峰，咱就是说，直接否了
    @GetMapping("/piliangUnStruct")
    @ResponseBody
    public ReturnModel piliangUnStruct(@RequestParam("path") String path,
                                       @RequestParam("tag") String tag) throws Exception {

        System.out.println(path);
        System.out.println(tag);
        //批量插入pdf
        //获取路径
        String folderPath = path; // 文件夹路径
        File folder = new File(folderPath);
        List<String> res = new ArrayList<>();
        // 检查文件夹是否存在
        if (folder.exists() && folder.isDirectory()) {
            res = PdfboxUtil.scanFolder(new ArrayList<String>(), folder);
        } else {
            System.out.println("文件夹不存在或者不是一个有效的文件夹路径！");
        }
        System.out.println(res.size());
        System.out.println(res);
        //处理每一个文件的插入

        String tagId = pdfimpl.getOrSetTag(tag);
        for(String pdfPath : res){
            filevo vo = PdfboxUtil.getFileVo(pdfPath);
            vo.setTagId(tagId);
            pdfimpl.insertPdf(vo);
            String contentRes = PdfboxUtil.getPdfContent(pdfPath);

            Long fileid = vo.getId();

            // 存文本信息
            contentVo cvo = new contentVo();
            cvo.setFileId(fileid);
            cvo.setContent(contentRes);
            pdfimpl.insertPdfContent(cvo);
        }
        String resStr = String.valueOf(res.size());
        resStr += "条数据已插入";
        return new ReturnModel(200, resStr, "");
    }
    //giao
    @PostMapping("/pdfToText")
    public ReturnModel health(@RequestParam("file") MultipartFile file, @RequestParam("name") String s,@RequestParam("tag") String tag) throws Exception {

        //1、参数检验
        System.out.println(file.getSize());
        System.out.println(s);

        //2、将客户上传文件存入服务器
        String os = System.getProperty("os.name").toLowerCase();
        String fileName = "D:\\TEMP_PDF\\"+ Instant.now().getEpochSecond()+file.getOriginalFilename();
        if(os.charAt(0) != 'w') fileName = "/fagai/java/pdffiles/"+ Instant.now().getEpochSecond()+file.getOriginalFilename();
        // 防止存储的文件重复 TODO
        File file1 = new File(fileName);
        /* 设置创建文件的时候,会生成不存在的目录*/
        file1.mkdirs();
        /*保存文件*/
        file.transferTo(file1);

//        String tagId = pdfimpl.getOrSetTag(tag);
        String tagId = tag;
        //3、实现读取PDF文字
        String res = PdfboxUtil.getPdfContent(fileName);
        //4、存入数据库 TODO
        filevo vo = PdfboxUtil.getFileVo(fileName);
        vo.setTagId(tagId);
//        System.out.println(vo.toString());
        // 存索引信息
        pdfimpl.insertPdf(vo);
        Long fileid = vo.getId();
//        System.out.println("文件ID");
//        System.out.println(fileid);
        // 存文本信息
        contentVo cvo = new contentVo();
        cvo.setFileId(fileid);
        cvo.setContent(res);
        pdfimpl.insertPdfContent(cvo);
        // 数据封装 TODO
        log.info("pdfToText success");
        return new ReturnModel(ReturnCodeEnum.getCode("success"), res, "");
    }
//        baseBiz.testMapper();
}
/*
1,批量读取pdf文件
2,可视化数据查询界面
3,逻辑化结构化的存储
    标识：衢江发改批〔2022〕30 号
4,集中化使用模型处理用户上传的文件
*/


